#coding=utf-8

import urllib

from common.import_common import *
from django.contrib.auth import login as auth_login, logout as auth_logout
from account.forms import LoginForm, ProfileForm, AvatarForm
from image.util import handle_upload_image
from image.models import Image


def login(request, template_name="account/login.html"):
    if request.method == "POST":
        form = LoginForm(request, data=request.POST)
        if form.is_valid(): # 这个地方来做认证处理          
            user = form.get_user()
            auth_login(request, user)    
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()   

            return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
        else:
            return HttpResponse("ok")
    else:
        form = LoginForm(request)
        return render_to_response(template_name, {
        'form': form,
        }, context_instance=RequestContext(request))        

@login_required
def logout(request,  template_name="account/logout.html"):
    auth_logout(request)
    return render_to_response(template_name, {},
                              context_instance=RequestContext(request))
    
    
     
    
def registry(request, template_name="account/registry.html"):
    if request.method == "GET":
        return HttpResponse("registry")
    
@login_required
def profile(request, template_name="account/profile.html"):
    user = request.user
    if request.method == "POST":
        form = ProfileForm(data = request.POST)
        if form.is_valid():
            form.save(user)            
            return render_to_response(template_name, {
                'form': form,
                'notice': u"修改成功"
                }, context_instance=RequestContext(request))
    else:
        form = ProfileForm(data={"gender":user.userprofile.gender,
                                 "relationship_status":user.userprofile.relationship_status,
                                 "birthday_year":user.userprofile.birthday_year,
                                 "birthday_month":user.userprofile.birthday_month,
                                 "birthday_day":user.userprofile.birthday_day,
                                 "country":user.userprofile.country,
                                 "location":user.userprofile.location,
                                 "introduction":user.userprofile.introduction})
     
    return render_to_response(template_name, {
            'form': form,
            'notice': u""
            }, context_instance=RequestContext(request))
            

@login_required
def avatar(request, template_name="account/avatar.html"):
    if request.method == 'POST':
        form = AvatarForm(request.POST, request.FILES)
        if form.is_valid():
            file = request.FILES['avatar']
            if file.size > settings.MAX_PHOTO_FILE_SIZE:
                message = u'上传文件大小不能超过2M'
                has_error = True
            else: 
                image =  handle_upload_image(request.user, request.FILES['avatar'], Image.PHOTO)
                userprofile = request.user.userprofile
                userprofile.avatar = image
                userprofile.save()
                return HttpResponseRedirect("/account/avatar/")
                
   
    else:
        form = AvatarForm()
    return render_to_response(template_name, {"form":form,},
                                  context_instance=RequestContext(request))          